package com.zkingsoft.rhi.controller;

import cn.hutool.crypto.SecureUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zkingsoft.rhi.entity.User;
import com.zkingsoft.rhi.handler.Result;
import com.zkingsoft.rhi.jwt.Jwt;
import com.zkingsoft.rhi.mapper.UserMapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author xieguo
 * @since 2020/11/4 14:07
 */

@Api(tags = "登录")
@RestController
@RequestMapping("/login")
public class LoginController {

    @Autowired
    UserMapper userMapper;

    /**
     * @author xieguo
     * 功能描述：登陆验证
     * @param user
     * @return com.alibaba.fastjson.JSONObject
     * @date 2020/11/10 11:18
     */
    @ApiOperation(value = "登录接口")
    @PostMapping("/login")
    public JSONObject login(@RequestBody User user){
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("account",user.getAccount());
        User user1 = userMapper.selectOne(wrapper);
        if (null==user1){
            return Result.setResultByCodeAndMsg(Result.STATUS_FAIL,"用户名不存在");
        }
//        System.out.println("getId================>"+user1.getId());
//        System.out.println("getUsername================>"+user1.getUsername());
        if (SecureUtil.md5(user.getPassword() + "password").equals(user1.getPassword())){
            String jwt = Jwt.BuildJwtString(user1.getId().toString(), user1.getUsername());
            return Result.setResultByCodeAndMsgAndDate(Result.STATUS_SUCCESS,"登录成功",jwt,null);
        }else {
            return Result.setResultByCodeAndMsg(Result.STATUS_FAIL,"用户名或密码错误");
        }

    }


}
